import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:何人亮
 * Date:2024-08-23
 * 14:55
 */
public class Test {
    public static void main(String[] args) {

    }
}
class Solution {
    public class TreeNode {
     int val;
     TreeNode left;
     TreeNode right;
     TreeNode() {}
     TreeNode(int val) { this.val = val; }
     TreeNode(int val, TreeNode left, TreeNode right) {
         this.val = val;
         this.left = left;
         this.right = right;
     }
  }
    public List<List<Integer>> levelOrderBottom(TreeNode root) {

        List<List<Integer>> ret=new ArrayList<>();
        Queue<TreeNode> queue=new LinkedList();
        if(root==null){
            return ret;
        }
        queue.offer(root);

        TreeNode cur= null;
        while (!queue.isEmpty()){
            List list=new ArrayList<>();
            int size= queue.size();
            while (size!=0){
                cur= queue.peek();
                TreeNode cur1=queue.poll();
                list.add(cur1.val);
                if(cur.left!=null){
                    queue.offer(cur.left);
                }
                if(cur.right!=null){
                    queue.offer(cur.right);
                }
                size--;
            }
            ret.add(list);
        }
        reverse(ret);
        return ret;
    }
    public void reverse(List<List<Integer>> ret){
        List<List<Integer>> ret1=new ArrayList<>();
        int size=ret.size();
        while (size!=0 ){
            ret1.add(ret.get(size-1));
            size--;
        }
    }
}